{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程2.2】  Pandas数据结构Series：基本概念及创建\n",
    "\n",
    "\"一维数组\"Serise\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.229773\n",
      "1    0.357622\n",
      "2    0.546116\n",
      "3    0.734517\n",
      "4    0.686645\n",
      "dtype: float64\n",
      "<class 'pandas.core.series.Series'>\n",
      "RangeIndex(start=0, stop=5, step=1) <class 'pandas.indexes.range.RangeIndex'>\n",
      "[ 0.22977307  0.35762236  0.54611623  0.73451707  0.68664496] <class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "# Series 数据结构\n",
    "# Series 是带有标签的一维数组，可以保存任何数据类型（整数，字符串，浮点数，Python对象等）,轴标签统称为索引\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd  \n",
    "# 导入numpy、pandas模块\n",
    "\n",
    "s = pd.Series(np.random.rand(5))\n",
    "print(s)\n",
    "print(type(s))\n",
    "# 查看数据、数据类型\n",
    "\n",
    "print(s.index,type(s.index))\n",
    "print(s.values,type(s.values))\n",
    "# .index查看series索引，类型为rangeindex\n",
    "# .values查看series值，类型是ndarray\n",
    "\n",
    "# 核心：series相比于ndarray，是一个自带索引index的数组 → 一维数组 + 对应索引\n",
    "# 所以当只看series的值的时候，就是一个ndarray\n",
    "# series和ndarray较相似，索引切片功能差别不大\n",
    "# series和dict相比，series更像一个有顺序的字典（dict本身不存在顺序），其索引原理与字典相似（一个用key，一个用index）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4    4\n",
      "5    5\n",
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# Series 创建方法一：由字典创建，字典的key就是index，values就是values\n",
    "\n",
    "dic = {'a':1 ,'b':2 , 'c':3, '4':4, '5':5}\n",
    "s = pd.Series(dic)\n",
    "print(s)\n",
    "# 注意：key肯定是字符串，假如values类型不止一个会怎么样？ → dic = {'a':1 ,'b':'hello' , 'c':3, '4':4, '5':5}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.11206121  0.1324684   0.59930544  0.34707543 -0.15652941]\n",
      "0    0.112061\n",
      "1    0.132468\n",
      "2    0.599305\n",
      "3    0.347075\n",
      "4   -0.156529\n",
      "dtype: float64\n",
      "a    0.112061\n",
      "b    0.132468\n",
      "c    0.599305\n",
      "d    0.347075\n",
      "e   -0.156529\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "# Series 创建方法二：由数组创建(一维数组)\n",
    "\n",
    "arr = np.random.randn(5)\n",
    "s = pd.Series(arr)\n",
    "print(arr)\n",
    "print(s)\n",
    "# 默认index是从0开始，步长为1的数字\n",
    "\n",
    "s = pd.Series(arr, index = ['a','b','c','d','e'],dtype = np.object)\n",
    "print(s)\n",
    "# index参数：设置index，长度保持一致\n",
    "# dtype参数：设置数值类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10\n",
      "1    10\n",
      "2    10\n",
      "3    10\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# Series 创建方法三：由标量创建\n",
    "\n",
    "s = pd.Series(10, index = range(4))\n",
    "print(s)\n",
    "# 如果data是标量值，则必须提供索引。该值会重复，来匹配索引的长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -0.403084\n",
      "1    1.369383\n",
      "2    1.134319\n",
      "3   -0.635050\n",
      "4    1.680211\n",
      "dtype: float64\n",
      "-----\n",
      "0   -0.120014\n",
      "1    1.967648\n",
      "2    1.142626\n",
      "3    0.234079\n",
      "4    0.761357\n",
      "Name: test, dtype: float64\n",
      "None test <class 'str'>\n",
      "0   -0.120014\n",
      "1    1.967648\n",
      "2    1.142626\n",
      "3    0.234079\n",
      "4    0.761357\n",
      "Name: hehehe, dtype: float64\n",
      "hehehe test\n"
     ]
    }
   ],
   "source": [
    "# Series 名称属性：name\n",
    "\n",
    "s1 = pd.Series(np.random.randn(5))\n",
    "print(s1)\n",
    "print('-----')\n",
    "s2 = pd.Series(np.random.randn(5),name = 'test')\n",
    "print(s2)\n",
    "print(s1.name, s2.name,type(s2.name))\n",
    "# name为Series的一个参数，创建一个数组的 名称\n",
    "# .name方法：输出数组的名称，输出格式为str，如果没用定义输出名称，输出为None\n",
    "\n",
    "s3 = s2.rename('hehehe')\n",
    "print(s3)\n",
    "print(s3.name, s2.name)\n",
    "# .rename()重命名一个数组的名称，并且新指向一个数组，原数组不变"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "######## 本节课有作业，请查看 “课程作业.docx”  ########"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程2.3】  Pandas数据结构Series：索引\n",
    "\n",
    "位置下标 / 标签索引 / 切片索引 / 布尔型索引\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.924575\n",
      "1    0.988654\n",
      "2    0.426333\n",
      "3    0.216504\n",
      "4    0.453570\n",
      "dtype: float64\n",
      "0.924575004833 <class 'numpy.float64'> float64\n",
      "0.9245750048328816 <class 'float'>\n"
     ]
    }
   ],
   "source": [
    "# 位置下标，类似序列\n",
    "\n",
    "s = pd.Series(np.random.rand(5))\n",
    "print(s)\n",
    "print(s[0],type(s[0]),s[0].dtype)\n",
    "print(float(s[0]),type(float(s[0])))\n",
    "#print(s[-1])\n",
    "# 位置下标从0开始\n",
    "# 输出结果为numpy.float格式，\n",
    "# 可以通过float()函数转换为python float格式\n",
    "# numpy.float与float占用字节不同\n",
    "# s[-1]结果如何？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0.714630\n",
      "b    0.213957\n",
      "c    0.172188\n",
      "d    0.972158\n",
      "e    0.875175\n",
      "dtype: float64\n",
      "0.714630383451 <class 'numpy.float64'> float64\n",
      "a    0.714630\n",
      "b    0.213957\n",
      "e    0.875175\n",
      "dtype: float64 <class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "source": [
    "# 标签索引\n",
    "\n",
    "s = pd.Series(np.random.rand(5), index = ['a','b','c','d','e'])\n",
    "print(s)\n",
    "print(s['a'],type(s['a']),s['a'].dtype)\n",
    "# 方法类似下标索引，用[]表示，内写上index，注意index是字符串\n",
    "\n",
    "sci = s[['a','b','e']]\n",
    "print(sci,type(sci))\n",
    "# 如果需要选择多个标签的值，用[[]]来表示（相当于[]中包含一个列表）\n",
    "# 多标签索引结果是新的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1    0.865967\n",
      "2    0.114500\n",
      "3    0.369301\n",
      "dtype: float64 0.411702342342\n",
      "a    0.717378\n",
      "b    0.642561\n",
      "c    0.391091\n",
      "dtype: float64 0.39109096261\n",
      "a    0.717378\n",
      "b    0.642561\n",
      "c    0.391091\n",
      "dtype: float64 0.998978363818\n",
      "-----\n",
      "a    0.717378\n",
      "b    0.642561\n",
      "c    0.391091\n",
      "d    0.998978\n",
      "dtype: float64\n",
      "a    0.717378\n",
      "c    0.391091\n",
      "e    0.957639\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 切片索引\n",
    "\n",
    "s1 = pd.Series(np.random.rand(5))\n",
    "s2 = pd.Series(np.random.rand(5), index = ['a','b','c','d','e'])\n",
    "print(s1[1:4],s1[4])\n",
    "print(s2['a':'c'],s2['c'])\n",
    "print(s2[0:3],s2[3])\n",
    "print('-----')\n",
    "# 注意：用index做切片是末端包含\n",
    "\n",
    "print(s2[:-1])\n",
    "print(s2[::2])\n",
    "# 下标索引做切片，和list写法一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    2.03802\n",
      "1    40.3989\n",
      "2    25.2001\n",
      "4       None\n",
      "dtype: object\n",
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "4    False\n",
      "dtype: bool <class 'pandas.core.series.Series'> bool\n",
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "4     True\n",
      "dtype: bool <class 'pandas.core.series.Series'> bool\n",
      "0     True\n",
      "1     True\n",
      "2     True\n",
      "4    False\n",
      "dtype: bool <class 'pandas.core.series.Series'> bool\n",
      "-----\n",
      "Series([], dtype: object)\n",
      "0    2.03802\n",
      "1    40.3989\n",
      "2    25.2001\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "# 布尔型索引\n",
    "\n",
    "s = pd.Series(np.random.rand(3)*100)\n",
    "s[4] = None  # 添加一个空值\n",
    "print(s)\n",
    "bs1 = s > 50\n",
    "bs2 = s.isnull()\n",
    "bs3 = s.notnull()\n",
    "print(bs1, type(bs1), bs1.dtype)\n",
    "print(bs2, type(bs2), bs2.dtype)\n",
    "print(bs3, type(bs3), bs3.dtype)\n",
    "print('-----')\n",
    "# 数组做判断之后，返回的是一个由布尔值组成的新的数组\n",
    "# .isnull() / .notnull() 判断是否为空值 (None代表空值，NaN代表有问题的数值，两个都会识别为空值)\n",
    "\n",
    "print(s[s > 50])\n",
    "print(s[bs3])\n",
    "# 布尔型索引方法：用[判断条件]表示，其中判断条件可以是 一个语句，或者是 一个布尔型数组！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "######## 本节课有作业，请查看 “课程作业.docx”  ########"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "【课程2.4】  Pandas数据结构Series：基本技巧\n",
    "\n",
    "数据查看 / 重新索引 / 对齐 / 添加、修改、删除值\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.730540\n",
      "1    0.116711\n",
      "2    0.787693\n",
      "3    0.969764\n",
      "4    0.324540\n",
      "5    0.061827\n",
      "6    0.377060\n",
      "7    0.820383\n",
      "8    0.964477\n",
      "9    0.451936\n",
      "dtype: float64\n",
      "45    0.899540\n",
      "46    0.237008\n",
      "47    0.298762\n",
      "48    0.848487\n",
      "49    0.829858\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 数据查看\n",
    "\n",
    "s = pd.Series(np.random.rand(50))\n",
    "print(s.head(10))\n",
    "print(s.tail())\n",
    "# .head()查看头部数据\n",
    "# .tail()查看尾部数据\n",
    "# 默认查看5条"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0.343718\n",
      "b    0.322228\n",
      "c    0.746720\n",
      "dtype: float64\n",
      "c    0.746720\n",
      "b    0.322228\n",
      "a    0.343718\n",
      "d         NaN\n",
      "dtype: float64\n",
      "c    0.746720\n",
      "b    0.322228\n",
      "a    0.343718\n",
      "d    0.000000\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 重新索引reindex\n",
    "# .reindex将会根据索引重新排序，如果当前索引不存在，则引入缺失值\n",
    "\n",
    "s = pd.Series(np.random.rand(3), index = ['a','b','c'])\n",
    "print(s)\n",
    "s1 = s.reindex(['c','b','a','d'])\n",
    "print(s1)\n",
    "# .reindex()中也是写列表\n",
    "# 这里'd'索引不存在，所以值为NaN\n",
    "\n",
    "s2 = s.reindex(['c','b','a','d'], fill_value = 0)\n",
    "print(s2)\n",
    "# fill_value参数：填充缺失值的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jack     0.753732\n",
      "Marry    0.180223\n",
      "Tom      0.283704\n",
      "dtype: float64\n",
      "Wang     0.309128\n",
      "Jack     0.533997\n",
      "Marry    0.626126\n",
      "dtype: float64\n",
      "Jack     1.287729\n",
      "Marry    0.806349\n",
      "Tom           NaN\n",
      "Wang          NaN\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# Series对齐\n",
    "\n",
    "s1 = pd.Series(np.random.rand(3), index = ['Jack','Marry','Tom'])\n",
    "s2 = pd.Series(np.random.rand(3), index = ['Wang','Jack','Marry'])\n",
    "print(s1)\n",
    "print(s2)\n",
    "print(s1+s2)\n",
    "# Series 和 ndarray 之间的主要区别是，Series 上的操作会根据标签自动对齐\n",
    "# index顺序不会影响数值计算，以标签来计算\n",
    "# 空值和任何值计算结果扔为空值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n    0.876587\n",
      "g    0.594053\n",
      "j    0.628232\n",
      "u    0.360634\n",
      "r    0.454483\n",
      "dtype: float64\n",
      "g    0.594053\n",
      "j    0.628232\n",
      "u    0.360634\n",
      "r    0.454483\n",
      "dtype: float64\n",
      "n    0.876587\n",
      "u    0.360634\n",
      "r    0.454483\n",
      "dtype: float64\n",
      "n    0.876587\n",
      "g    0.594053\n",
      "j    0.628232\n",
      "u    0.360634\n",
      "r    0.454483\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 删除：.drop\n",
    "\n",
    "s = pd.Series(np.random.rand(5), index = list('ngjur'))\n",
    "print(s)\n",
    "s1 = s.drop('n')\n",
    "s2 = s.drop(['g','j'])\n",
    "print(s1)\n",
    "print(s2)\n",
    "print(s)\n",
    "# drop 删除元素之后返回副本(inplace=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.516447\n",
      "1    0.699382\n",
      "2    0.469513\n",
      "3    0.589821\n",
      "4    0.402188\n",
      "dtype: float64\n",
      "n    0.615641\n",
      "g    0.451192\n",
      "j    0.022328\n",
      "u    0.977568\n",
      "r    0.902041\n",
      "dtype: float64\n",
      "0      0.516447\n",
      "1      0.699382\n",
      "2      0.469513\n",
      "3      0.589821\n",
      "4      0.402188\n",
      "5    100.000000\n",
      "dtype: float64\n",
      "n      0.615641\n",
      "g      0.451192\n",
      "j      0.022328\n",
      "u      0.977568\n",
      "r      0.902041\n",
      "a    100.000000\n",
      "dtype: float64\n",
      "-----\n",
      "0      0.516447\n",
      "1      0.699382\n",
      "2      0.469513\n",
      "3      0.589821\n",
      "4      0.402188\n",
      "5    100.000000\n",
      "n      0.615641\n",
      "g      0.451192\n",
      "j      0.022328\n",
      "u      0.977568\n",
      "r      0.902041\n",
      "a    100.000000\n",
      "dtype: float64\n",
      "0      0.516447\n",
      "1      0.699382\n",
      "2      0.469513\n",
      "3      0.589821\n",
      "4      0.402188\n",
      "5    100.000000\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 添加\n",
    "\n",
    "s1 = pd.Series(np.random.rand(5))\n",
    "s2 = pd.Series(np.random.rand(5), index = list('ngjur'))\n",
    "print(s1)\n",
    "print(s2)\n",
    "s1[5] = 100\n",
    "s2['a'] = 100\n",
    "print(s1)\n",
    "print(s2)\n",
    "print('-----')\n",
    "# 直接通过下标索引/标签index添加值\n",
    "\n",
    "s3 = s1.append(s2)\n",
    "print(s3)\n",
    "print(s1)\n",
    "# 通过.append方法，直接添加一个数组\n",
    "# .append方法生成一个新的数组，不改变之前的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0.873604\n",
      "b    0.244707\n",
      "c    0.888685\n",
      "dtype: float64\n",
      "a    100.0\n",
      "b    200.0\n",
      "c    200.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 修改\n",
    "\n",
    "s = pd.Series(np.random.rand(3), index = ['a','b','c'])\n",
    "print(s)\n",
    "s['a'] = 100\n",
    "s[['b','c']] = 200\n",
    "print(s)\n",
    "# 通过索引直接修改，类似序列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "######## 本节课有作业，请查看 “课程作业.docx”  ########"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程2.5】  Pandas数据结构Dataframe：基本概念及创建\n",
    "\n",
    "\"二维数组\"Dataframe：是一个表格型的数据结构，包含一组有序的列，其列的值类型可以是数值、字符串、布尔值等。\n",
    "\n",
    "Dataframe中的数据以一个或多个二维块存放，不是列表、字典或一维数组结构。\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   age gender  name\n",
      "0   18      m  Jack\n",
      "1   19      m   Tom\n",
      "2   20      w  Mary\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex(start=0, stop=3, step=1) \n",
      "该数据类型为： <class 'pandas.indexes.range.RangeIndex'>\n",
      "Index(['age', 'gender', 'name'], dtype='object') \n",
      "该数据类型为： <class 'pandas.indexes.base.Index'>\n",
      "[[18 'm' 'Jack']\n",
      " [19 'm' 'Tom']\n",
      " [20 'w' 'Mary']] \n",
      "该数据类型为： <class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "# Dataframe 数据结构\n",
    "# Dataframe是一个表格型的数据结构，“带有标签的二维数组”。\n",
    "# Dataframe带有index（行标签）和columns（列标签）\n",
    "\n",
    "data = {'name':['Jack','Tom','Mary'],\n",
    "        'age':[18,19,20],\n",
    "       'gender':['m','m','w']}\n",
    "frame = pd.DataFrame(data)\n",
    "print(frame)  \n",
    "print(type(frame))\n",
    "print(frame.index,'\\n该数据类型为：',type(frame.index))\n",
    "print(frame.columns,'\\n该数据类型为：',type(frame.columns))\n",
    "print(frame.values,'\\n该数据类型为：',type(frame.values))\n",
    "# 查看数据，数据类型为dataframe\n",
    "# .index查看行标签\n",
    "# .columns查看列标签\n",
    "# .values查看值，数据类型为ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': [1, 2, 3], 'c': [5, 6, 7], 'b': [3, 4, 5]}\n",
      "{'one': array([ 0.00101091,  0.08807153,  0.58345056]), 'two': array([ 0.49774634,  0.16782565,  0.76443489])}\n",
      "   a  b  c\n",
      "0  1  3  5\n",
      "1  2  4  6\n",
      "2  3  5  7\n",
      "        one       two\n",
      "0  0.001011  0.497746\n",
      "1  0.088072  0.167826\n",
      "2  0.583451  0.764435\n",
      "   b  c  a    d\n",
      "0  3  5  1  NaN\n",
      "1  4  6  2  NaN\n",
      "2  5  7  3  NaN\n",
      "   b  c\n",
      "0  3  5\n",
      "1  4  6\n",
      "2  5  7\n",
      "         one       two\n",
      "f1  0.001011  0.497746\n",
      "f2  0.088072  0.167826\n",
      "f3  0.583451  0.764435\n"
     ]
    }
   ],
   "source": [
    "# Dataframe 创建方法一：由数组/list组成的字典\n",
    "# 创建方法:pandas.Dataframe()\n",
    "\n",
    "data1 = {'a':[1,2,3],\n",
    "        'b':[3,4,5],\n",
    "        'c':[5,6,7]}\n",
    "data2 = {'one':np.random.rand(3),\n",
    "        'two':np.random.rand(3)}   # 这里如果尝试  'two':np.random.rand(4) 会怎么样？\n",
    "print(data1)\n",
    "print(data2)\n",
    "df1 = pd.DataFrame(data1)\n",
    "df2 = pd.DataFrame(data2)\n",
    "print(df1)\n",
    "print(df2)\n",
    "# 由数组/list组成的字典 创建Dataframe，columns为字典key，index为默认数字标签\n",
    "# 字典的值的长度必须保持一致！\n",
    "\n",
    "df1 = pd.DataFrame(data1, columns = ['b','c','a','d'])\n",
    "print(df1)\n",
    "df1 = pd.DataFrame(data1, columns = ['b','c'])\n",
    "print(df1)\n",
    "# columns参数：可以重新指定列的顺序，格式为list，如果现有数据中没有该列（比如'd'），则产生NaN值\n",
    "# 如果columns重新指定时候，列的数量可以少于原数据\n",
    "\n",
    "df2 = pd.DataFrame(data2, index = ['f1','f2','f3'])  # 这里如果尝试  index = ['f1','f2','f3','f4'] 会怎么样？\n",
    "print(df2)\n",
    "# index参数：重新定义index，格式为list，长度必须保持一致"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'one': 0    0.892580\n",
      "1    0.834076\n",
      "dtype: float64, 'two': 0    0.301309\n",
      "1    0.977709\n",
      "2    0.489000\n",
      "dtype: float64}\n",
      "{'one': a    0.470947\n",
      "b    0.584577\n",
      "dtype: float64, 'two': a    0.122659\n",
      "b    0.136429\n",
      "c    0.396825\n",
      "dtype: float64}\n",
      "        one       two\n",
      "0  0.892580  0.301309\n",
      "1  0.834076  0.977709\n",
      "2       NaN  0.489000\n",
      "        one       two\n",
      "a  0.470947  0.122659\n",
      "b  0.584577  0.136429\n",
      "c       NaN  0.396825\n"
     ]
    }
   ],
   "source": [
    "# Dataframe 创建方法二：由Series组成的字典\n",
    "\n",
    "data1 = {'one':pd.Series(np.random.rand(2)),\n",
    "        'two':pd.Series(np.random.rand(3))}  # 没有设置index的Series\n",
    "data2 = {'one':pd.Series(np.random.rand(2), index = ['a','b']),\n",
    "        'two':pd.Series(np.random.rand(3),index = ['a','b','c'])}  # 设置了index的Series\n",
    "print(data1)\n",
    "print(data2)\n",
    "df1 = pd.DataFrame(data1)\n",
    "df2 = pd.DataFrame(data2)\n",
    "print(df1)\n",
    "print(df2)\n",
    "# 由Seris组成的字典 创建Dataframe，columns为字典key，index为Series的标签（如果Series没有指定标签，则是默认数字标签）\n",
    "# Series可以长度不一样，生成的Dataframe会出现NaN值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.54492282  0.28956161  0.46592269]\n",
      " [ 0.30480674  0.12917132  0.38757672]\n",
      " [ 0.2518185   0.13544544  0.13930429]]\n",
      "          0         1         2\n",
      "0  0.544923  0.289562  0.465923\n",
      "1  0.304807  0.129171  0.387577\n",
      "2  0.251819  0.135445  0.139304\n",
      "        one       two     three\n",
      "a  0.544923  0.289562  0.465923\n",
      "b  0.304807  0.129171  0.387577\n",
      "c  0.251819  0.135445  0.139304\n"
     ]
    }
   ],
   "source": [
    "# Dataframe 创建方法三：通过二维数组直接创建\n",
    "\n",
    "ar = np.random.rand(9).reshape(3,3)\n",
    "print(ar)\n",
    "df1 = pd.DataFrame(ar)\n",
    "df2 = pd.DataFrame(ar, index = ['a', 'b', 'c'], columns = ['one','two','three'])  # 可以尝试一下index或columns长度不等于已有数组的情况\n",
    "print(df1)\n",
    "print(df2)\n",
    "# 通过二维数组直接创建Dataframe，得到一样形状的结果数据，如果不指定index和columns，两者均返回默认数字格式\n",
    "# index和colunms指定长度与原数组保持一致"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'one': 1, 'two': 2}, {'one': 5, 'three': 20, 'two': 10}]\n",
      "   one  three  two\n",
      "0    1    NaN    2\n",
      "1    5   20.0   10\n",
      "   one  three  two\n",
      "a    1    NaN    2\n",
      "b    5   20.0   10\n",
      "   one  two\n",
      "0    1    2\n",
      "1    5   10\n"
     ]
    }
   ],
   "source": [
    "# Dataframe 创建方法四：由字典组成的列表\n",
    "\n",
    "data = [{'one': 1, 'two': 2}, {'one': 5, 'two': 10, 'three': 20}]\n",
    "print(data)\n",
    "df1 = pd.DataFrame(data)\n",
    "df2 = pd.DataFrame(data, index = ['a','b'])\n",
    "df3 = pd.DataFrame(data, columns = ['one','two'])\n",
    "print(df1)\n",
    "print(df2)\n",
    "print(df3)\n",
    "# 由字典组成的列表创建Dataframe，columns为字典的key，index不做指定则为默认数组标签\n",
    "# colunms和index参数分别重新指定相应列及行标签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         Jack  Marry   Tom\n",
      "art        78     92   NaN\n",
      "english    89     95  67.0\n",
      "math       90     82  78.0\n",
      "         Jack   Tom  Bob\n",
      "art        78   NaN  NaN\n",
      "english    89  67.0  NaN\n",
      "math       90  78.0  NaN\n",
      "   Jack  Marry  Tom\n",
      "a   NaN    NaN  NaN\n",
      "b   NaN    NaN  NaN\n",
      "c   NaN    NaN  NaN\n"
     ]
    }
   ],
   "source": [
    "# Dataframe 创建方法五：由字典组成的字典\n",
    "\n",
    "data = {'Jack':{'math':90,'english':89,'art':78},\n",
    "       'Marry':{'math':82,'english':95,'art':92},\n",
    "       'Tom':{'math':78,'english':67}}\n",
    "df1 = pd.DataFrame(data)\n",
    "print(df1)\n",
    "# 由字典组成的字典创建Dataframe，columns为字典的key，index为子字典的key\n",
    "\n",
    "df2 = pd.DataFrame(data, columns = ['Jack','Tom','Bob'])\n",
    "df3 = pd.DataFrame(data, index = ['a','b','c'])\n",
    "print(df2)\n",
    "print(df3)\n",
    "# columns参数可以增加和减少现有列，如出现新的列，值为NaN\n",
    "# index在这里和之前不同，并不能改变原有index，如果指向新的标签，值为NaN （非常重要！）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "######## 本节课有作业，请查看 “课程作业.docx”  ########"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程2.6】  Pandas数据结构Dataframe：索引\n",
    "\n",
    "Dataframe既有行索引也有列索引，可以被看做由Series组成的字典（共用一个索引）\n",
    "\n",
    "选择列 / 选择行 / 切片 / 布尔判断\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "               a          b          c          d\n",
      "one    72.615321  49.816987  57.485645  84.226944\n",
      "two    46.295674  34.480439  92.267989  17.111412\n",
      "three  14.699591  92.754997  39.683577  93.255880\n",
      "one      72.615321\n",
      "two      46.295674\n",
      "three    14.699591\n",
      "Name: a, dtype: float64 <class 'pandas.core.series.Series'>\n",
      "               a          c\n",
      "one    72.615321  57.485645\n",
      "two    46.295674  92.267989\n",
      "three  14.699591  39.683577 <class 'pandas.core.frame.DataFrame'>\n",
      "-----\n",
      "               a          c\n",
      "one    72.615321  57.485645\n",
      "two    46.295674  92.267989\n",
      "three  14.699591  39.683577 <class 'pandas.core.series.Series'>\n",
      "a    72.615321\n",
      "b    49.816987\n",
      "c    57.485645\n",
      "d    84.226944\n",
      "Name: one, dtype: float64 <class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "source": [
    "# 选择行与列\n",
    "\n",
    "df = pd.DataFrame(np.random.rand(12).reshape(3,4)*100,\n",
    "                   index = ['one','two','three'],\n",
    "                   columns = ['a','b','c','d'])\n",
    "print(df)\n",
    "\n",
    "data1 = df['a']\n",
    "data2 = df[['a','c']]\n",
    "print(data1,type(data1))\n",
    "print(data2,type(data2))\n",
    "print('-----')\n",
    "# 按照列名选择列，只选择一列输出Series，选择多列输出Dataframe\n",
    "\n",
    "data3 = df.loc['one']\n",
    "data4 = df.loc[['one','two']]\n",
    "print(data2,type(data3))\n",
    "print(data3,type(data4))\n",
    "# 按照index选择行，只选择一行输出Series，选择多行输出Dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "               a          b          c          d\n",
      "one    88.490183  93.588825   1.605172  74.610087\n",
      "two    45.905361  49.257001  87.852426  97.490521\n",
      "three  95.801001  97.991028  74.451954  64.290587\n",
      "-----\n",
      "one      88.490183\n",
      "two      45.905361\n",
      "three    95.801001\n",
      "Name: a, dtype: float64\n",
      "               b          c\n",
      "one    93.588825   1.605172\n",
      "two    49.257001  87.852426\n",
      "three  97.991028  74.451954\n",
      "             a          b         c          d\n",
      "one  88.490183  93.588825  1.605172  74.610087 <class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "source": [
    "# df[] - 选择列\n",
    "# 一般用于选择列，也可以选择行\n",
    "\n",
    "df = pd.DataFrame(np.random.rand(12).reshape(3,4)*100,\n",
    "                   index = ['one','two','three'],\n",
    "                   columns = ['a','b','c','d'])\n",
    "print(df)\n",
    "print('-----')\n",
    "\n",
    "data1 = df['a']\n",
    "data2 = df[['b','c']]  # 尝试输入 data2 = df[['b','c','e']]\n",
    "print(data1)\n",
    "print(data2)\n",
    "# df[]默认选择列，[]中写列名（所以一般数据colunms都会单独制定，不会用默认数字列名，以免和index冲突）\n",
    "# 单选列为Series，print结果为Series格式\n",
    "# 多选列为Dataframe，print结果为Dataframe格式\n",
    "\n",
    "data3 = df[:1]\n",
    "#data3 = df[0]\n",
    "#data3 = df['one']\n",
    "print(data3,type(data3))\n",
    "# df[]中为数字时，默认选择行，且只能进行切片的选择，不能单独选择（df[0]）\n",
    "# 输出结果为Dataframe，即便只选择一行\n",
    "# df[]不能通过索引标签名来选择行(df['one'])\n",
    "\n",
    "# 核心笔记：df[col]一般用于选择列，[]中写列名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "               a          b          c          d\n",
      "one    73.070679   7.169884  80.820532  62.299367\n",
      "two    34.025462  77.849955  96.160170  55.159017\n",
      "three  27.897582  39.595687  69.280955  49.477429\n",
      "four   76.723039  44.995970  22.408450  23.273089\n",
      "           a          b          c          d\n",
      "0  93.871055  28.031989  57.093181  34.695293\n",
      "1  22.882809  47.499852  86.466393  86.140909\n",
      "2  80.840336  98.120735  84.495414   8.413039\n",
      "3  59.695834   1.478707  15.069485  48.775008\n",
      "-----\n",
      "a    73.070679\n",
      "b     7.169884\n",
      "c    80.820532\n",
      "d    62.299367\n",
      "Name: one, dtype: float64\n",
      "a    22.882809\n",
      "b    47.499852\n",
      "c    86.466393\n",
      "d    86.140909\n",
      "Name: 1, dtype: float64\n",
      "单标签索引\n",
      "-----\n",
      "               a          b          c          d\n",
      "two    34.025462  77.849955  96.160170  55.159017\n",
      "three  27.897582  39.595687  69.280955  49.477429\n",
      "five         NaN        NaN        NaN        NaN\n",
      "           a          b          c          d\n",
      "3  59.695834   1.478707  15.069485  48.775008\n",
      "2  80.840336  98.120735  84.495414   8.413039\n",
      "1  22.882809  47.499852  86.466393  86.140909\n",
      "多标签索引\n",
      "-----\n",
      "               a          b          c          d\n",
      "one    73.070679   7.169884  80.820532  62.299367\n",
      "two    34.025462  77.849955  96.160170  55.159017\n",
      "three  27.897582  39.595687  69.280955  49.477429\n",
      "           a          b          c          d\n",
      "1  22.882809  47.499852  86.466393  86.140909\n",
      "2  80.840336  98.120735  84.495414   8.413039\n",
      "3  59.695834   1.478707  15.069485  48.775008\n",
      "切片索引\n"
     ]
    }
   ],
   "source": [
    "# df.loc[] - 按index选择行\n",
    "\n",
    "df1 = pd.DataFrame(np.random.rand(16).reshape(4,4)*100,\n",
    "                   index = ['one','two','three','four'],\n",
    "                   columns = ['a','b','c','d'])\n",
    "df2 = pd.DataFrame(np.random.rand(16).reshape(4,4)*100,\n",
    "                   columns = ['a','b','c','d'])\n",
    "print(df1)\n",
    "print(df2)\n",
    "print('-----')\n",
    "\n",
    "data1 = df1.loc['one']\n",
    "data2 = df2.loc[1]\n",
    "print(data1)\n",
    "print(data2)\n",
    "print('单标签索引\\n-----')\n",
    "# 单个标签索引，返回Series\n",
    "\n",
    "data3 = df1.loc[['two','three','five']]\n",
    "data4 = df2.loc[[3,2,1]]\n",
    "print(data3)\n",
    "print(data4)\n",
    "print('多标签索引\\n-----')\n",
    "# 多个标签索引，如果标签不存在，则返回NaN\n",
    "# 顺序可变\n",
    "\n",
    "data5 = df1.loc['one':'three']\n",
    "data6 = df2.loc[1:3]\n",
    "print(data5)\n",
    "print(data6)\n",
    "print('切片索引')\n",
    "# 可以做切片对象\n",
    "# 末端包含\n",
    "\n",
    "# 核心笔记：df.loc[label]主要针对index选择行，同时支持指定index，及默认数字index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "               a          b          c          d\n",
      "one    21.848926   2.482328  17.338355  73.014166\n",
      "two    99.092794   0.601173  18.598736  61.166478\n",
      "three  87.183015  85.973426  48.839267  99.930097\n",
      "four   75.007726  84.208576  69.445779  75.546038\n",
      "------\n",
      "a    21.848926\n",
      "b     2.482328\n",
      "c    17.338355\n",
      "d    73.014166\n",
      "Name: one, dtype: float64\n",
      "a    75.007726\n",
      "b    84.208576\n",
      "c    69.445779\n",
      "d    75.546038\n",
      "Name: four, dtype: float64\n",
      "单位置索引\n",
      "-----\n",
      "               a          b          c          d\n",
      "one    21.848926   2.482328  17.338355  73.014166\n",
      "three  87.183015  85.973426  48.839267  99.930097\n",
      "               a          b          c          d\n",
      "four   75.007726  84.208576  69.445779  75.546038\n",
      "three  87.183015  85.973426  48.839267  99.930097\n",
      "two    99.092794   0.601173  18.598736  61.166478\n",
      "多位置索引\n",
      "-----\n",
      "               a          b          c          d\n",
      "two    99.092794   0.601173  18.598736  61.166478\n",
      "three  87.183015  85.973426  48.839267  99.930097\n",
      "               a          b          c          d\n",
      "one    21.848926   2.482328  17.338355  73.014166\n",
      "three  87.183015  85.973426  48.839267  99.930097\n",
      "切片索引\n"
     ]
    }
   ],
   "source": [
    "# df.iloc[] - 按照整数位置（从轴的0到length-1）选择行\n",
    "# 类似list的索引，其顺序就是dataframe的整数位置，从0开始计\n",
    "\n",
    "df = pd.DataFrame(np.random.rand(16).reshape(4,4)*100,\n",
    "                   index = ['one','two','three','four'],\n",
    "                   columns = ['a','b','c','d'])\n",
    "print(df)\n",
    "print('------')\n",
    "\n",
    "print(df.iloc[0])\n",
    "print(df.iloc[-1])\n",
    "#print(df.iloc[4])\n",
    "print('单位置索引\\n-----')\n",
    "# 单位置索引\n",
    "# 和loc索引不同，不能索引超出数据行数的整数位置\n",
    "\n",
    "print(df.iloc[[0,2]])\n",
    "print(df.iloc[[3,2,1]])\n",
    "print('多位置索引\\n-----')\n",
    "# 多位置索引\n",
    "# 顺序可变\n",
    "\n",
    "print(df.iloc[1:3])\n",
    "print(df.iloc[::2])\n",
    "print('切片索引')\n",
    "# 切片索引\n",
    "# 末端不包含"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "               a          b          c          d\n",
      "one    19.185849  20.303217  21.800384  45.189534\n",
      "two    50.105112  28.478878  93.669529  90.029489\n",
      "three  35.496053  19.248457  74.811841  20.711431\n",
      "four   24.604478  57.731456  49.682717  82.132866\n",
      "------\n",
      "           a      b      c      d\n",
      "one     True  False  False  False\n",
      "two    False  False  False  False\n",
      "three  False   True  False  False\n",
      "four   False  False  False  False <class 'pandas.core.frame.DataFrame'>\n",
      "               a          b   c   d\n",
      "one    19.185849        NaN NaN NaN\n",
      "two          NaN        NaN NaN NaN\n",
      "three        NaN  19.248457 NaN NaN\n",
      "four         NaN        NaN NaN NaN\n",
      "------\n",
      "one      False\n",
      "two       True\n",
      "three    False\n",
      "four     False\n",
      "Name: a, dtype: bool <class 'pandas.core.series.Series'>\n",
      "             a          b          c          d\n",
      "two  50.105112  28.478878  93.669529  90.029489\n",
      "------\n",
      "           a      b\n",
      "one    False  False\n",
      "two     True  False\n",
      "three  False  False\n",
      "four   False   True <class 'pandas.core.frame.DataFrame'>\n",
      "               a          b   c   d\n",
      "one          NaN        NaN NaN NaN\n",
      "two    50.105112        NaN NaN NaN\n",
      "three        NaN        NaN NaN NaN\n",
      "four         NaN  57.731456 NaN NaN\n",
      "------\n",
      "          a     b      c     d\n",
      "one    True  True   True  True\n",
      "three  True  True  False  True <class 'pandas.core.frame.DataFrame'>\n",
      "               a          b          c          d\n",
      "one    19.185849  20.303217  21.800384  45.189534\n",
      "two          NaN        NaN        NaN        NaN\n",
      "three  35.496053  19.248457        NaN  20.711431\n",
      "four         NaN        NaN        NaN        NaN\n",
      "------\n"
     ]
    }
   ],
   "source": [
    "# 布尔型索引\n",
    "# 和Series原理相同\n",
    "\n",
    "df = pd.DataFrame(np.random.rand(16).reshape(4,4)*100,\n",
    "                   index = ['one','two','three','four'],\n",
    "                   columns = ['a','b','c','d'])\n",
    "print(df)\n",
    "print('------')\n",
    "\n",
    "b1 = df < 20\n",
    "print(b1,type(b1))\n",
    "print(df[b1])  # 也可以书写为 df[df < 20]\n",
    "print('------')\n",
    "# 不做索引则会对数据每个值进行判断\n",
    "# 索引结果保留 所有数据：True返回原数据，False返回值为NaN\n",
    "\n",
    "b2 = df['a'] > 50\n",
    "print(b2,type(b2))\n",
    "print(df[b2])  # 也可以书写为 df[df['a'] > 50]\n",
    "print('------')\n",
    "# 单列做判断\n",
    "# 索引结果保留 单列判断为True的行数据，包括其他列\n",
    "\n",
    "b3 = df[['a','b']] > 50\n",
    "print(b3,type(b3))\n",
    "print(df[b3])  # 也可以书写为 df[df[['a','b']] > 50]\n",
    "print('------')\n",
    "# 多列做判断\n",
    "# 索引结果保留 所有数据：True返回原数据，False返回值为NaN\n",
    "\n",
    "b4 = df.loc[['one','three']] < 50\n",
    "print(b4,type(b4))\n",
    "print(df[b4])  # 也可以书写为 df[df.loc[['one','three']] < 50]\n",
    "print('------')\n",
    "# 多行做判断\n",
    "# 索引结果保留 所有数据：True返回原数据，False返回值为NaN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "               a          b          c          d\n",
      "one    50.660904  89.827374  51.096827   3.844736\n",
      "two    70.699721  78.750014  52.988276  48.833037\n",
      "three  33.653032  27.225202  24.864712  29.662736\n",
      "four   21.792339  26.450939   6.122134  52.323963\n",
      "------\n",
      "one      50.660904\n",
      "three    33.653032\n",
      "Name: a, dtype: float64\n",
      "               b          c          d\n",
      "one    89.827374  51.096827   3.844736\n",
      "three  27.225202  24.864712  29.662736\n",
      "               a          b          c          d\n",
      "three  33.653032  27.225202  24.864712  29.662736\n",
      "four   21.792339  26.450939   6.122134  52.323963\n"
     ]
    }
   ],
   "source": [
    "# 多重索引：比如同时索引行和列\n",
    "# 先选择列再选择行 —— 相当于对于一个数据，先筛选字段，再选择数据量\n",
    "\n",
    "df = pd.DataFrame(np.random.rand(16).reshape(4,4)*100,\n",
    "                   index = ['one','two','three','four'],\n",
    "                   columns = ['a','b','c','d'])\n",
    "print(df)\n",
    "print('------')\n",
    "\n",
    "print(df['a'].loc[['one','three']])   # 选择a列的one，three行\n",
    "print(df[['b','c','d']].iloc[::2])   # 选择b，c，d列的one，three行\n",
    "print(df[df['a'] < 50].iloc[:2])   # 选择满足判断索引的前两行数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "######## 本节课有作业，请查看 “课程作业.docx”  ########"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程2.7】  Pandas数据结构Dataframe：基本技巧\n",
    "\n",
    "数据查看、转置 / 添加、修改、删除值 / 对齐 / 排序\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           a          b\n",
      "0   5.777208  18.374283\n",
      "1  85.961515  55.120036\n",
      "           a          b\n",
      "3  21.236577  15.902872\n",
      "4  46.137564  29.350647\n",
      "5  70.157709  58.972728\n",
      "6   8.368292  42.011356\n",
      "7  29.824574  87.062295\n",
      "           0          1          2          3          4          5  \\\n",
      "a   5.777208  85.961515  11.005284  21.236577  46.137564  70.157709   \n",
      "b  18.374283  55.120036  35.595598  15.902872  29.350647  58.972728   \n",
      "\n",
      "           6          7  \n",
      "a   8.368292  29.824574  \n",
      "b  42.011356  87.062295  \n"
     ]
    }
   ],
   "source": [
    "# 数据查看、转置\n",
    "\n",
    "df = pd.DataFrame(np.random.rand(16).reshape(8,2)*100,\n",
    "                   columns = ['a','b'])\n",
    "print(df.head(2))\n",
    "print(df.tail())\n",
    "# .head()查看头部数据\n",
    "# .tail()查看尾部数据\n",
    "# 默认查看5条\n",
    "\n",
    "print(df.T)\n",
    "# .T 转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           a          b          c          d\n",
      "0  17.148791  73.833921  39.069417   5.675815\n",
      "1  91.572695  66.851601  60.320698  92.071097\n",
      "2  79.377105  24.314520  44.406357  57.313429\n",
      "3  84.599206  61.310945   3.916679  30.076458\n",
      "           a          b          c          d   e\n",
      "0  17.148791  73.833921  39.069417   5.675815  10\n",
      "1  91.572695  66.851601  60.320698  92.071097  10\n",
      "2  79.377105  24.314520  44.406357  57.313429  10\n",
      "3  84.599206  61.310945   3.916679  30.076458  10\n",
      "4  20.000000  20.000000  20.000000  20.000000  20\n",
      "     a          b    c          d   e\n",
      "0  100  73.833921  100   5.675815  20\n",
      "1  100  66.851601  100  92.071097  20\n",
      "2  100  24.314520  100  57.313429  20\n",
      "3  100  61.310945  100  30.076458  20\n",
      "4  100  20.000000  100  20.000000  20\n"
     ]
    }
   ],
   "source": [
    "# 添加与修改\n",
    "\n",
    "df = pd.DataFrame(np.random.rand(16).reshape(4,4)*100,\n",
    "                   columns = ['a','b','c','d'])\n",
    "print(df)\n",
    "\n",
    "df['e'] = 10\n",
    "df.loc[4] = 20\n",
    "print(df)\n",
    "# 新增列/行并赋值\n",
    "\n",
    "df['e'] = 20\n",
    "df[['a','c']] = 100\n",
    "print(df)\n",
    "# 索引后直接修改值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           a          b          c          d\n",
      "0  91.866806  88.753655  18.469852  71.651277\n",
      "1  64.835568  33.844967   6.391246  54.916094\n",
      "2  75.930985  19.169862  91.042457  43.648258\n",
      "3  15.863853  24.788866  10.625684  82.135316\n",
      "           b          c          d\n",
      "0  88.753655  18.469852  71.651277\n",
      "1  33.844967   6.391246  54.916094\n",
      "2  19.169862  91.042457  43.648258\n",
      "3  24.788866  10.625684  82.135316\n",
      "-----\n",
      "           b          c          d\n",
      "1  33.844967   6.391246  54.916094\n",
      "2  19.169862  91.042457  43.648258\n",
      "3  24.788866  10.625684  82.135316\n",
      "           b          c          d\n",
      "0  88.753655  18.469852  71.651277\n",
      "3  24.788866  10.625684  82.135316\n",
      "           b          c          d\n",
      "0  88.753655  18.469852  71.651277\n",
      "1  33.844967   6.391246  54.916094\n",
      "2  19.169862  91.042457  43.648258\n",
      "3  24.788866  10.625684  82.135316\n",
      "-----\n",
      "           b          c\n",
      "0  88.753655  18.469852\n",
      "1  33.844967   6.391246\n",
      "2  19.169862  91.042457\n",
      "3  24.788866  10.625684\n",
      "           b          c          d\n",
      "0  88.753655  18.469852  71.651277\n",
      "1  33.844967   6.391246  54.916094\n",
      "2  19.169862  91.042457  43.648258\n",
      "3  24.788866  10.625684  82.135316\n"
     ]
    }
   ],
   "source": [
    "# 删除  del / drop()\n",
    "\n",
    "df = pd.DataFrame(np.random.rand(16).reshape(4,4)*100,\n",
    "                   columns = ['a','b','c','d'])\n",
    "print(df)\n",
    "\n",
    "del df['a']\n",
    "print(df)\n",
    "print('-----')\n",
    "# del语句 - 删除列\n",
    "\n",
    "print(df.drop(0))\n",
    "print(df.drop([1,2]))\n",
    "print(df)\n",
    "print('-----')\n",
    "# drop()删除行，inplace=False → 删除后生成新的数据，不改变原数据\n",
    "\n",
    "print(df.drop(['d'], axis = 1))\n",
    "print(df)\n",
    "# drop()删除列，需要加上axis = 1，inplace=False → 删除后生成新的数据，不改变原数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          A         B         C   D\n",
      "0 -0.281123 -2.529461  1.325663 NaN\n",
      "1 -0.310514 -0.408225 -0.760986 NaN\n",
      "2 -0.172169 -2.355042  1.521342 NaN\n",
      "3  1.113505  0.325933  3.689586 NaN\n",
      "4  0.107513 -0.503907 -1.010349 NaN\n",
      "5 -0.845676 -2.410537 -1.406071 NaN\n",
      "6  1.682854 -0.576620 -0.981622 NaN\n",
      "7       NaN       NaN       NaN NaN\n",
      "8       NaN       NaN       NaN NaN\n",
      "9       NaN       NaN       NaN NaN\n"
     ]
    }
   ],
   "source": [
    "# 对齐\n",
    "\n",
    "df1 = pd.DataFrame(np.random.randn(10, 4), columns=['A', 'B', 'C', 'D'])\n",
    "df2 = pd.DataFrame(np.random.randn(7, 3), columns=['A', 'B', 'C'])\n",
    "print(df1 + df2)\n",
    "# DataFrame对象之间的数据自动按照列和索引（行标签）对齐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           a          b          c          d\n",
      "0  16.519099  19.601879  35.464189  58.866972\n",
      "1  34.506472  97.106578  96.308244  54.049359\n",
      "2  87.177828  47.253416  92.098847  19.672678\n",
      "3  66.673226  51.969534  71.789055  14.504191\n",
      "           a          b          c          d\n",
      "0  16.519099  19.601879  35.464189  58.866972\n",
      "1  34.506472  97.106578  96.308244  54.049359\n",
      "3  66.673226  51.969534  71.789055  14.504191\n",
      "2  87.177828  47.253416  92.098847  19.672678\n",
      "           a          b          c          d\n",
      "2  87.177828  47.253416  92.098847  19.672678\n",
      "3  66.673226  51.969534  71.789055  14.504191\n",
      "1  34.506472  97.106578  96.308244  54.049359\n",
      "0  16.519099  19.601879  35.464189  58.866972\n",
      "------\n",
      "   a  b  c\n",
      "0  1  0  8\n",
      "1  1  1  7\n",
      "2  1  2  6\n",
      "3  1  3  5\n",
      "4  2  4  4\n",
      "5  2  5  3\n",
      "6  2  6  2\n",
      "7  2  7  1\n",
      "   a  b  c\n",
      "3  1  3  5\n",
      "2  1  2  6\n",
      "1  1  1  7\n",
      "0  1  0  8\n",
      "7  2  7  1\n",
      "6  2  6  2\n",
      "5  2  5  3\n",
      "4  2  4  4\n"
     ]
    }
   ],
   "source": [
    "# 排序1 - 按值排序 .sort_values\n",
    "# 同样适用于Series\n",
    "\n",
    "df1 = pd.DataFrame(np.random.rand(16).reshape(4,4)*100,\n",
    "                   columns = ['a','b','c','d'])\n",
    "print(df1)\n",
    "print(df1.sort_values(['a'], ascending = True))  # 升序\n",
    "print(df1.sort_values(['a'], ascending = False))  # 降序\n",
    "print('------')\n",
    "# ascending参数：设置升序降序，默认升序\n",
    "# 单列排序\n",
    "\n",
    "df2 = pd.DataFrame({'a':[1,1,1,1,2,2,2,2],\n",
    "                  'b':list(range(8)),\n",
    "                  'c':list(range(8,0,-1))})\n",
    "print(df2)\n",
    "print(df2.sort_values(['a','c']))\n",
    "# 多列排序，按列顺序排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           a          b          c          d\n",
      "5  57.327269  87.623119  93.655538   5.859571\n",
      "4  69.739134  80.084366  89.005538  56.825475\n",
      "3  88.148296   6.211556  68.938504  41.542563\n",
      "2  29.248036  72.005306  57.855365  45.931715\n",
      "           a          b          c          d\n",
      "2  29.248036  72.005306  57.855365  45.931715\n",
      "3  88.148296   6.211556  68.938504  41.542563\n",
      "4  69.739134  80.084366  89.005538  56.825475\n",
      "5  57.327269  87.623119  93.655538   5.859571\n",
      "           a          b          c          d\n",
      "h  50.579469  80.239138  24.085110  39.443600\n",
      "s  30.906725  39.175302  11.161542  81.010205\n",
      "x  19.900056  18.421110   4.995141  12.605395\n",
      "g  67.760755  72.573568  33.507090  69.854906\n",
      "           a          b          c          d\n",
      "g  67.760755  72.573568  33.507090  69.854906\n",
      "h  50.579469  80.239138  24.085110  39.443600\n",
      "s  30.906725  39.175302  11.161542  81.010205\n",
      "x  19.900056  18.421110   4.995141  12.605395\n"
     ]
    }
   ],
   "source": [
    "# 排序2 - 索引排序 .sort_index\n",
    "\n",
    "df1 = pd.DataFrame(np.random.rand(16).reshape(4,4)*100,\n",
    "                  index = [5,4,3,2],\n",
    "                   columns = ['a','b','c','d'])\n",
    "df2 = pd.DataFrame(np.random.rand(16).reshape(4,4)*100,\n",
    "                  index = ['h','s','x','g'],\n",
    "                   columns = ['a','b','c','d'])\n",
    "print(df1)\n",
    "print(df1.sort_index())\n",
    "print(df2)\n",
    "print(df2.sort_index())\n",
    "# 按照index排序\n",
    "# 默认 ascending=True, inplace=False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "######## 本节课有作业，请查看 “课程作业.docx”  ########"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
